package org.kotboot.kit;/**
 * Created by jisong.sun on 2017/9/14.
 */


import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * jisong.sun 2017/9/14 10:28
 */
public class K {

    public static boolean isNull(Object o){
        return o == null;
    }
    public static boolean notNull(Object o){
        return isNull(o);
    }
    public static String toJson(Object o){
        return JSON.toJSONString(o);
    }
    public static <T> T toBean(String json,Class<T> clazz){
        return JSON.parseObject(json,clazz);
    }

    public static <T> T toBean(String json,Type type){
        return JSON.parseObject(json,type);
    }

    public static KMap toMap(String json){
        return (KMap) JSON.parseObject(json);
    }
    public static <T> List<T> toList(String json, Class<T> clazz){
        return JSON.parseArray(json,clazz);
    }
    public static String toStr(Object o) {
        return isNull(o)?"":o.toString();
    }
    public static <T> T transBean(Object resource,Class<T> clazz){
        return K.toBean(toJson(resource),clazz);
    }

    public static <T> T transBean(Object resource,Type type){
        return K.toBean(toJson(resource),type);
    }
    public static boolean isEmpty(Object o){
        if(isNull(o)){ return true;}
        if(o instanceof String){
            return Str.isEmpty((String)o);
        }
        if(o instanceof Map){
            return ((Map)o).isEmpty();
        }
        if(o instanceof Collection){
            return ((Collection)o).isEmpty();
        }
        if(o.getClass().isArray()){
            return Arrays.asList(o).isEmpty();
        }
        return false;
    }

    public static boolean notEmpty(Object o){
        return !isEmpty(o);
    }
    public static class http {
        public static void toUTF8(HttpServletResponse resp){
            resp.setHeader("Content-type", "text/html;charset=UTF-8");
            resp.setCharacterEncoding("UTF-8");
        }

        public static KMap castMap(HttpServletRequest req){
            KMap map = new KMap();
            Map<String, String[]> param = req.getParameterMap();
            if(K.isEmpty(param)){return map;}
            for(String key : param.keySet()){
                String[] vals = param.get(key);
                if(vals.length == 1){
                    map.put(key,vals[0]);
                }else{
                    map.put(key,vals);
                }
            }
            return map;
        }
    }

    public static class Str{
        public static String toUTF8(String s){
            return new String(s.getBytes(Charset.forName("UTF-8")));
        }
        public static boolean isEmpty(String s){
            return isNull(s) || s.trim().length() <= 0 || "null".equals(s);
        }
        public static String encryptMobile(String mobile){
            if(isEmpty(mobile)) {return mobile;}
            return StringUtils.replacePattern(mobile,"^(\\d{3}).*(\\d{4})$","$1****$2");
        }
    }


    public static void main(String[] args){
        String s = Str.encryptMobile("13123456789");
        System.out.println(s);
    }
}
